package com.android.accessibility;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.android.accessibility.utils.Log;

public class NetworkManager {
    public static final int NETWORK_TYPE_WIFI = 1;
    public static final int NETWORK_TYPE_2G = 2;
    public static final int NETWORK_TYPE_3G = 3;
    public static final int NETWORK_TYPE_4G = 4;

    private Context mContext;

    private static class SingletonFactory {
        private static NetworkManager sNetworkManager = new NetworkManager();
    }

    private NetworkManager() {
    }

    public static NetworkManager get(Context context) {
        SingletonFactory.sNetworkManager.initContext(context);
        return SingletonFactory.sNetworkManager;
    }

    private void initContext(Context context) {
        mContext = context;
    }

    public boolean networkConnected() {
        ConnectivityManager connectMgr = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectMgr.getActiveNetworkInfo();
        if (info != null) {
            return true;
        }
        return false;
    }

    public boolean shouldDisplayMiddleImage() {
        ConnectivityManager connectMgr = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectMgr.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return true;
    }

    public boolean shouldDisplayBigImage() {
        return shouldDisplayMiddleImage();
    }

    public int getNetworkType() {
        int netType = 0;
        ConnectivityManager connectMgr = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                netType = NETWORK_TYPE_WIFI;
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                String subTypeName = networkInfo.getSubtypeName();
                // TD-SCDMA networkType is 17
                int networkType = networkInfo.getSubtype();
                switch (networkType) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case 11:// TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace
                        // by 11
                    netType = NETWORK_TYPE_2G;
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case 14:// TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 replace
                        // by 14
                case 12:// TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 replace
                        // by 12
                case 15:// TelephonyManager.NETWORK_TYPE_HSPAP: //api<13 replace
                        // by 15
                    netType = NETWORK_TYPE_3G;
                    break;
                case 13:// TelephonyManager.NETWORK_TYPE_LTE: //api<11 replace
                        // by 13
                    netType = NETWORK_TYPE_4G;
                    break;
                default:
                    if (subTypeName.equalsIgnoreCase("TD-SCDMA")
                            || subTypeName.equalsIgnoreCase("WCDMA")
                            || subTypeName.equalsIgnoreCase("CDMA2000")) {
                        netType = NETWORK_TYPE_3G;
                    } else {
                        netType = 0;
                    }
                    break;
                }
                Log.e(Log.TAG, "networkType : " + networkType);
            }
        }
        return netType;
    }

}
